<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Model\Gift;
use App\Util\AliyunUtil;
use App\Util\ComUtil;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;

class GiftController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('礼物列表');
            $content->description('');
            $content->body($this->grid());
        });
    }

    /**
     * 编辑
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('礼物编辑');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('添加礼物');
            $content->description('');
            $content->body($this->form());
        });
    }

    protected function grid()
    {
        return Admin::grid(Gift::class, function (Grid $grid) {
            $grid->model()->orderBy('created_at', 'desc');
            $grid->id('礼物ID');
            $grid->name('礼物名');
            $grid->order_index('排序（0-255，越小越靠前）')->editable()->sortable();
            $grid->coin('价值金币')->editable()->sortable();
            $grid->type('礼物图片')->display(function ($data) {
                return Gift::TYPE[$data];
            });
            $grid->icon('礼物图片')->display(function ($data) {
                if (!$data) return '';
                $pics = explode(',', $data);
                return ComUtil::buidlImgaeHtml($pics, 200, 200, true);
            });
            $grid->created_at('创建时间')->display(function ($time) {
                return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
            });
            $grid->flag('状态')->display(function ($data) {
                return $data == 1 ? '有效' : '下架';
            });

            $grid->filter(function ($filter) {
                $filter->expand();
                $filter->disableIdFilter();
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('type', '礼物类型')->select(Gift::TYPE);
                });
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('flag', '礼物状态')->select([0 => '下架', 1 => '上架']);
                });
            });

            //禁用导出
            $grid->disableExport();
            //禁用行选择
            $grid->disableRowSelector();
            // $grid->disableFilter();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableView();
            });
        });
    }

    /**
     * 新建
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Gift::class, function (Form $form) {
            $form->text('name', '礼物名')->default('');
            $form->text('coin', '金币数')->default(0);
            $form->text('order_index', '排序')->default(1);
            $form->image('icon', '礼物图片')->uniqueName();
            $form->file('show_image', '特效图片')->uniqueName();
            $form->select('type', '礼物类型')->default(1)->options(Gift::TYPE);
            $form->select('flag', '是否开启')->default(1)->options([0 => '禁用', 1 => '启用']);
        });
    }

    /**
     * 操作
     */
    function op()
    {
        $id = request('id');
        $flag = request('status');
        // AppVersion::where('id', $id)->limit(1)->update(['flag' => $flag]);
        return ['ret' => 0, 'message' => '操作成功！'];
    }
}
